Introduction to data science in R

Lesson 8: Putting it all together




Brian S. Evans, Ph.D.
Migratory Bird Center
Smithsonian Conservation Biology Institute



Setup for the lesson


# Load RCurl library:

library(RCurl)

# Load a source script:

script <-
  getURL(
    paste0(
      'https://raw.githubusercontent.com/bsevansunc/workshop_languageOfR/master/',
      'sourceCode_workshop.R'
    )
  )

# Evaluate then remove the source script:

eval(parse(text = script))

rm(script)

Today’s goals


Today’s goals


  • Bells and whistles in R studio
    • Code sections
    • R Projects
    • R Notebooks and Markdown
  • Sourcing code
  • Github
    • What is version control?
    • The bash environment
    • gitBash


icons

R Studio: Code sections


Code sections: Break larger file into discrete regions

  • Improve workflow and readability
  • Simplify code navigation
  • Foldable

Insert section head:

# Section 1 

# Section 1 -----

# Section 1 ----*

R Studio: Code sections


Code sections: Break larger file into discrete regions

  • Improve workflow and readability
  • Simplify code navigation
  • Foldable

Insert section head using CTRL(CMD)+SHIFT+R:

icons

R Studio: Code sections


Code sections: Break larger file into discrete regions

  • Improve workflow and readability
  • Simplify code navigation
  • Foldable

Insert section head: Ctrl(Cmd)+Shift+R:

icons

R Studio: Code sections


Code sections: Break larger file into discrete regions

  • Improve workflow and readability
  • Simplify code navigation
  • Foldable

icons

R Studio: Code sections


Code sections: Break larger file into discrete regions

  • Improve workflow and readability
  • Simplify code navigation
  • Foldable


icons

R Studio: Code sections


Code sections: Break larger file into discrete regions

  • Improve workflow and readability
  • Simplify code navigation
  • Foldable

Jump to: Shift+Alt+J

icons

R Studio: Code sections


Code sections: Break larger file into discrete regions

  • Improve workflow and readability
  • Simplify code navigation
  • Foldable


icons

R Studio: Code sections


Code sections: Break larger file into discrete regions

  • Improve workflow and readability
  • Simplify code navigation
  • Foldable

Fold all: Alt+O

icons

R Studio: Code sections


Code sections: Break larger file into discrete regions

  • Improve workflow and readability
  • Simplify code navigation
  • Foldable

Expand

icons

R Studio: Code sections


Code sections: Break larger file into discrete regions

  • Improve workflow and readability
  • Simplify code navigation
  • Foldable

Expand all: Shift+Alt+O

icons

Exercise One:


Explore the code sections of the file sourceCode_workshop.R. Use this file to do the following:

  1. The data frames birdCounts and birdMeasures are in the same code section. Provide separate code sections for each data frame.
  2. Explore folding and unfolding these code sections.

R Studio: R Projects


If you work on multiple projects, it is useful to separate tasks into R projects. An R project has its own:

  • Environment
  • Working directory
  • History

icons

R Studio: R Projects


icons

R Studio: R Projects


icons

R Studio: R Projects


icons

Exercise Two:



Create a project in the directory where you are running the R files for this class.

R Studio: R Notebooks and Markdown


R Notebooks are digital lab notebooks written in Markdown, a plain text formatting language.

icons

R Studio: R Notebooks and Markdown


R Notebooks are digital lab notebooks written in Markdown, a plain text formatting language.

icons

R Studio: R Notebooks and Markdown


R Markdown documents

icons

R Studio: R Notebooks and Markdown


R Markdown documents

icons

R Studio: R Notebooks and Markdown


R Markdown documents

icons

R Studio: R Notebooks and Markdown


R Markdown documents

icons

R Studio: R Notebooks and Markdown


R Markdown documents

icons

R Studio: R Notebooks and Markdown


R Markdown documents can also be written in html (like all of the presentations in this workshop).

icons

Exercise Three:



Create a new R notebook. Copy-and-paste the code from the functions code section of sourceCodeWorkshop.R into the notebook document. Replace the comments with markdown text and wrap code sections for evaluation.

Sourcing code


Scripts called source code can be run from within another script.

  • Use global source code for steps you run for every project
  • Split large, multi-step projects into multiple scripts
    1. Use project-level source code for long or multi-step projects
    2. Connect scripts with source code

icons

Sourcing code


Scripts called source code can be run from within another script.

  • Use global source code for steps you run for every project
  • Split large, multi-step projects into multiple scripts
    1. Use project-level source code for long or multi-step projects
    2. Connect scripts using sourcing
source('sourceCode_workshop.R')

Exercise Four:


  1. Use the keyboard shortcut Ctrl(Cmd)+Shift+N to open a blank worksheet.
  2. Copy-and-paste the following function into the worksheet:
  3. printHw <- function(){
      "Hello world!"
    }
  4. Save the worksheet as sourceFunctions.R in your current working directory.
  5. Use the function printHw to remove the function from your environment.
  6. Type the following into your console:
  7. source(`sourceFunctions.R`)
    
    printHw()